package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.PageInfo;
import com.itheima.common.ThreadLocalIUtil;
import com.itheima.common.UserPageInfo;
import com.itheima.dto.OrderDto;
import com.itheima.mapper.AddressMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import com.itheima.vo.OrderVo;
import com.itheima.vo.PageOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public PageInfo<Order> page(String number, LocalDateTime beginTmie, LocalDateTime endTime, Integer page, Integer pageSize) {
        PageInfo<Order> pageInfo = new PageInfo<>();
        Integer start = (page - 1) * pageSize;
        List<Order> orders = orderMapper.page(number,beginTmie,endTime,start, pageSize);
        pageInfo.setRecords(orders);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        long count = orderMapper.count();
        pageInfo.setTotal(count);
        return pageInfo;
    }

    @Override
    public boolean updateStatus(String id, int status) {
        int res = orderMapper.updateStatus(id, status);
       return res>0;
    }

    @Override
    public boolean ClientSubmit(OrderDto orderDto) {
        Order order=new Order();
        BeanUtils.copyProperties(orderDto,order);
        String OrderId = IdUtil.getSnowflakeNextIdStr();
        order.setId(OrderId);
        order.setNumber(OrderId);
        //获取用户信息
        String userId = ThreadLocalIUtil.getId();
        order.setUserId(userId);
        User user = userMapper.findById(userId);
        order.setUserName(user.getName());

        order.setPhone(user.getPhone());
        //从购物车获取数据
        List<ShoppingCart> list = shoppingCartMapper.getList();
        int totalPrices=0;
        for (ShoppingCart commodity : list) {
            Integer amount = commodity.getAmount();
            Integer number = commodity.getNumber();
           Integer price= amount * number;
            totalPrices=price+totalPrices;
        }
        order.setAmount(totalPrices);
        //设置下单 付款时间
        order.setOrderTime(LocalDateTime.now());
       //付款时间为付款为null
        // order.setCheckoutTime();
        Address address = addressMapper.findByUserId(ThreadLocalIUtil.getId());
        order.setAddress(address.getDetail());
        order.setConsignee(address.getConsignee());
        order.setStatus(1);
        boolean res = orderMapper.ClientSubmit(order);
        return res;


    }

    @Override
    public UserPageInfo<PageOrderVo> userPage(Integer page, Integer pageSize) {
            UserPageInfo<PageOrderVo> UserPageInfo=new UserPageInfo<>();
        Integer start =(page-1) * pageSize;
        List<Order> orderList = orderMapper.getOrderList(start, pageSize);

        List<PageOrderVo> voList=new ArrayList<>();

        for (Order order : orderList) {
            OrderVo orderVo=new OrderVo();
            BeanUtils.copyProperties(order,orderVo);

        }
        UserPageInfo.setRecords(voList);
        return UserPageInfo;
    }
}
